/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Game.java
 *
 * Created on 11.03.2012, 15:23:03
 */
package gui;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import writer.XmlWriter;

import bean.QuestionBean;
import dao.DaQuestionXML;
import dao.Dao;
import dao.DaoQuestionDB;

/**
 * 
 * @author root
 */
public class Game extends javax.swing.JFrame {

	private int mode;
	protected int time;
	protected int timeStatic;
	private Timer timer1;
	private QuestionBean guestionBeanCurent;
	private int score = 0;
	private Dao question;
	private ArrayList<Integer> id;

	public Game(int mode, int time) throws SQLException,
			InstantiationException, IllegalAccessException,
			ClassNotFoundException {

		this.mode = mode;
		this.time = time;
		this.timeStatic = time;

		question = new DaoQuestionDB();

		id = question.getAllId();

		initComponents();
		setNewQuestions();

		timer();
	}

	private void timer() {

		long delay1 = 1000;

		timer1 = new Timer();
		timer1.schedule(new ChangeTime(), 0, delay1);

	}

	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		jRadioButton1 = new javax.swing.JRadioButton();
		jLabel1 = new javax.swing.JLabel();
		jRadioButton2 = new javax.swing.JRadioButton();
		jRadioButton3 = new javax.swing.JRadioButton();
		jRadioButton4 = new javax.swing.JRadioButton();

		// ButtonModel model = new ButtonModel();

		jTextField1 = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		OkButton = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("Time :");

		jTextField1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField1ActionPerformed(evt);
			}
		});

		OkButton.addActionListener(new java.awt.event.ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				OkButtonActionPErformed();

			}

		});

		jLabel2.setText("");

		OkButton.setText("OK");

		jButton2.setText("Exit");

		jLabel3.setText(Integer.toString(score++));

		jLabel4.setText("Scorul");

		jLabel5.setText("");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(jRadioButton4)
												.addComponent(jRadioButton3)
												.addComponent(jRadioButton2)
												.addComponent(jRadioButton1)
												.addComponent(
														jTextField1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														291,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel1)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		jLabel2,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		103,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jLabel1)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		jLabel2,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		103,
																		javax.swing.GroupLayout.PREFERRED_SIZE))
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		OkButton)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		jButton2)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		118,
																		Short.MAX_VALUE)
																.addComponent(
																		jLabel4)
																.addGap(31, 31,
																		31)
																.addComponent(
																		jLabel3,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		50,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(jLabel2))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jTextField1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(jRadioButton1)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jRadioButton2)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jRadioButton3)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jRadioButton4)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(OkButton)
												.addComponent(jButton2)
												.addComponent(jLabel3)
												.addComponent(jLabel4))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		buttonGroup1.add(jRadioButton1);
		buttonGroup1.add(jRadioButton2);
		buttonGroup1.add(jRadioButton3);
		buttonGroup1.add(jRadioButton4);
		// buttonGroup1.setSelected(m, b)

		jTextField1.setEditable(false);

		pack();
	}

	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jTextField1ActionPerformed
		// TODO add your handling code here:
	}

//	protected void finalize() throws Throwable {
//		
//		super.finalize(); // not necessary if extending Object.
//	}

	private void setNewQuestions() throws SQLException, InstantiationException,
			IllegalAccessException, ClassNotFoundException {
		if (id.size() == 0) {
			XmlWriter.getIstance().saveXml();
			JOptionPane.showMessageDialog(this, "Sau terminat raspunsurile");
			 System.exit(0);
		}
		Dao question = new DaoQuestionDB();

		guestionBeanCurent = question.getQuestionBeanById(id.get(0));

		jRadioButton1.setText(guestionBeanCurent.getResponse1());

		jRadioButton2.setText(guestionBeanCurent.getResponse2());

		jRadioButton3.setText(guestionBeanCurent.getResponse3());

		jRadioButton4.setText(guestionBeanCurent.getResponse4());

		jTextField1.setText(guestionBeanCurent.getQuestion());

		jRadioButton1.setSelected(false);
		jRadioButton2.setSelected(false);
		jRadioButton3.setSelected(false);
		jRadioButton4.setSelected(false);

		id.remove(0);

	}

	private void OkButtonActionPErformed() {

		try {
			if (checkAnswer()) {
				jLabel3.setText(Integer.toString(score++));
				XmlWriter.getIstance().addNode(guestionBeanCurent, ansver,
						score);
				setNewQuestions();
			} else {
				JOptionPane.showMessageDialog(this, "Raspunsul e Gresit");
				score = 0;
				jLabel3.setText(Integer.toString(score));
				XmlWriter.getIstance().addNode(guestionBeanCurent, ansver,
						score);
			}

			if (mode == 1) {
				time = timeStatic;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private boolean checkAnswer() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {

		ansver = null;

		for (Enumeration e = buttonGroup1.getElements(); e.hasMoreElements();) {
			JRadioButton b = (JRadioButton) e.nextElement();
			if (b.getModel() == buttonGroup1.getSelection()) {
				ansver = b.getText();
			}
		}

		if (ansver.equals(guestionBeanCurent.getTrueResponse())) {
			return true;
		}
		return false;

	}

	class MyTimer extends TimerTask {

		@Override
		public void run() {

			try {
				setNewQuestions();

			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	public javax.swing.JFrame getThis() {
		return this;
	}

	public class ChangeTime extends TimerTask {

		@Override
		public void run() {
			// System.out.println("test");
			jLabel2.setText(Integer.toString(time--));
			if (time < 0) {
				XmlWriter.getIstance().saveXml();
				JOptionPane.showMessageDialog(getThis(), "Sa terminat timpul");
				System.exit(0);
			}

		}

	}

	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JButton OkButton;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JRadioButton jRadioButton1;
	private javax.swing.JRadioButton jRadioButton2;
	private javax.swing.JRadioButton jRadioButton3;
	private javax.swing.JRadioButton jRadioButton4;
	private javax.swing.JTextField jTextField1;
	// End of variables declaration//GEN-END:variables
	private String ansver;
}
